//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//
//using System.Net;
//using System.IO;
//using System.Threading;
//using UnityEngine;
//using Frankfort.Threading.Internal;
//using System.Runtime.InteropServices;
//
//using AOT;
//
//public class EvonyHttpDownloadHelper2
//{
//	private delegate void ProgressCallBack(int sum, int curCount, int curCount2, int curCount3, int first, float speed);
//
//	private const int DECOMPRESS_ERROR = -1;
//	private const int NOT_ENOUGH_SPACE = -2;
//
//	//brotli
////	#if UNITY_EDITOR
//	private static IntPtr create(){return IntPtr.Zero;}
//	private static void destroy(IntPtr curl){}
//	private static bool canDecompress(uint comprssType){return false;}
//	private static int download(IntPtr curl, string url, string content, int offset, int downloadSize, uint comprssType, int bufSize, int fileBufSize){return 0;}
//	private static int getProgress(IntPtr curl){return 0;}
//	private static void cleanup(){}
////	#else
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern IntPtr create();
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern int destroy(IntPtr curl);
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern int download(IntPtr curl, string url, string content, int offset, int downloadSize, uint comprssType, int bufSize, int fileBufSize);
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern int getProgress(IntPtr curl);
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern void cleanup();
////	#if UNITY_ANDROID
////	[DllImport("brotli")]
////	#else
////	[DllImport("__Internal")]
////	#endif
////	private static extern bool canDecompress(uint comprssType);
////	#endif
//
//	public delegate void SetStep(DownloadStep step);
//	public delegate void RepsonseAddTempSum(int sum);
//	public delegate void RepsonseSuccess();
//	public delegate void ResponseError(DownloadError status);
//
//	private const int DefaultTimeout = 5 * 1000;
//	private const int BUFFER_SIZE = 512 * 1024;
//	private const bool testFileLog = true;
////	private System.IntPtr _request;
//	private bool _isCancel;
//	private bool writeTimeLog;
//	private int _progress;
//
//	public int progress
//	{
//		get
//		{
//			IntPtr ptr = curl;
//			if(ptr != IntPtr.Zero)
//			{
//				int temp = getProgress(ptr);
//				_progress = Math.Max(temp, _progress);
//			}
//			return _progress;
//		}
//	}
//
//	private static Queue<IntPtr> curlList = new Queue<IntPtr>();
//	public static int getTcpQueueSize()
//	{
//		lock(curlList)
//		{
//			return curlList.Count;
//		}
//	}
//
//	static IntPtr createCurl ()
//	{
//		IntPtr curl;
//		lock(curlList)
//		{
//			if(curlList.Count > 0)
//			{
//				curl = curlList.Dequeue();
//			}
//			else
//			{
//				curl = create ();
//			}
//		}
//		return curl;
//	}
//
//	static void recycleCurl (IntPtr curl)
//	{
//		if(curl == IntPtr.Zero)
//		{
//			return;
//		}
//
//		lock(curlList)
//		{
//			curlList.Enqueue(curl);
//		}
//	}
//
//	//url: redirected url
//	//_localPath: the path using to save downloading data
//	//_originalUrl: original url, use for log
//	//_offset: offset of big file
//	//_size: download size, size of small file
//	//_totalSize: size of big file
//	//readedSum: aleardy downloaded size, size of small file
//	//setStep: set step
//	//repsonseAddTempSum: progress call back
//	//repsonseSuccess: success call back
//	//responseError: error call back
//	IntPtr curl;
//	public void download(bool createThread, string url, string _localPath, string _originalUrl, uint _offset, uint _size, uint _decompressSize, uint compressType, int readedSum, uint _totalSize, SetStep setStep, RepsonseSuccess repsonseSuccess, ResponseError responseError)
//	{
//		if(createThread)
//		{
//			Thread _workThread = null;
//			_workThread = SingleThreadStarter.StartSingleThread(() =>
//			{
//				realDownload (url, _localPath, _size, _decompressSize, compressType, readedSum, repsonseSuccess, responseError);
//			});
//			_workThread.Name = _originalUrl + "|" + _offset + "|" + _size;
//		}
//		else
//		{
//			realDownload (url, _localPath, _size, _decompressSize, compressType, readedSum, repsonseSuccess, responseError);
//		}
//	}
//
//	void realDownload (string url, string _localPath, uint _size, uint _decompressSize, uint compressType, int readedSum, RepsonseSuccess repsonseSuccess, ResponseError responseError)
//	{
//		bool downloaded = false;
//		DownloadError error = DownloadError.None;
//		do {
//			if (_isCancel)
//			{
//				break;
//			}
//
//			if (_size == readedSum)
//			{
//				downloaded = true;
//				break;
//			}
//
//			curl = createCurl ();
//			if (curl != IntPtr.Zero)
//			{
//				int ret = download (curl, url, _localPath, 0, (int)_size, compressType, 64 * 1024, 5 * 1024 * 1024);
//
//				uint fileSize = 0;
////				if(ret == 0)
////				{
////					fileSize = (uint)CommonUtil.GetFileSize(_localPath);
////					if(fileSize != (canDecompress(compressType)? _decompressSize: _size))
////					{
////						ret = NOT_ENOUGH_SPACE;
////					}
////				}
//
//				downloaded = ret == 0;
//				if (ret != 0)
//				{
//					if (Debug.enableLog) Debug.LogError ("test_ret:" + ret + " " + url + " " + fileSize + " " + canDecompress(compressType) + " " + _decompressSize + " " + _size);
//
//					if(ret == NOT_ENOUGH_SPACE)
//					{
//						error = DownloadError.FileOperationFailed;
//					}
//
//					if(ret == DECOMPRESS_ERROR)
//					{
//						error = DownloadError.DecompressFailed;
//					}
//				}
//				recycleCurl (curl);
//				curl = IntPtr.Zero;
//			}
//		}
//		while (false);
//		if (downloaded)
//		{
//			repsonseSuccess ();
//		}
//		else
//		{
//			if (error == DownloadError.None)
//			{
//				error = DownloadError.Unknown;
//			}
//			responseError (error);
//		}
//	}
//
//	public void Abort()
//	{
//		this._isCancel = true;
////		if(this._request != null)
////		{
////			this._request.Abort();
////		}
//	}
//
//	public static void cleanAllCurl()
//	{
//		lock(curlList)
//		{
//			while(curlList.Count > 0)
//			{
//				IntPtr curl = curlList.Dequeue();
//				destroy(curl);
//			}
//		}
//
//		cleanup();
//	}
//}
